package com.biyao.service.model.state.imp;
import com.baomidou.mybatisplus.MybatisAbstractSQL;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.biyao.schedulingdao.state.TGrindingLogMapper;
import com.biyao.schedulingentity.base.TGrindWheelBasicEntity;
import com.biyao.schedulingentity.base.TGrinderEntity;
import com.biyao.schedulingentity.base.TRollerBasicEntity;
import com.biyao.schedulingentity.product.TProductionPlanIEntity;
import com.biyao.schedulingentity.state.*;
import com.biyao.service.model.base.TGrindWheelBasicService;
import com.biyao.service.model.base.TGrinderMessage;
import com.biyao.service.model.base.TRollerBasicMessage;
import com.biyao.service.model.product.ProductionPlanIMessage;
import com.biyao.service.model.state.GrindingLogMessage;
import com.biyao.service.model.state.RollerExtMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 *
 * @author code generator
 * @date 2019-09-16 09:44:18
 */
@Service
public class TGrindingLogService extends ServiceImpl<TGrindingLogMapper, TGrindingLogEntity> implements GrindingLogMessage {

    @Autowired
    private RollerExtMessage rollerExtMessage;

    @Autowired
    private TGrinderMessage tGrinderMessage;
    @Autowired
    private TRollerBasicMessage tRollerBasicMessage;



    @Autowired
    private ProductionPlanIMessage productionPlanIMessage;

    @Autowired
    private TGrindWheelBasicService tGrindWheelBasicService;




    public Page list(int pageNum, int pageSize){

        EntityWrapper<TGrindingLogEntity> tGrindingLogentityWrapper=new EntityWrapper<>();
        Page page=selectPage(new Page(pageNum,pageSize),tGrindingLogentityWrapper);
        page.setRecords(formatRecords(page.getRecords()));
        return page;
    }

    List formatRecords(List<TGrindingLogEntity> list){
        List resList=new ArrayList();
        for(TGrindingLogEntity tGrindingLogEntity:list){
            Map map=formatEntity(tGrindingLogEntity);
            resList.add(map);
        }
        return resList;
    }

    Map formatEntity(TGrindingLogEntity tGrindingLogEntity){
        Map map=new HashMap(16);
        //磨辊日志
        if(tGrindingLogEntity.getId()!=null){
           map.put("id",tGrindingLogEntity.getId());
        }
        //t_roller_basic.id
        if(tGrindingLogEntity.getRollerId()!=null){
           map.put("rollerId",tGrindingLogEntity.getRollerId());
           TRollerBasicEntity tRollerBasicEntity=tRollerBasicMessage.selectRollerBasicById(tGrindingLogEntity.getRollerId());
           if(tRollerBasicEntity!=null){
               map.put("rollerCode",tRollerBasicEntity.getCode());
           }
        }
        //t_grinder.id
        if(tGrindingLogEntity.getGrinderId()!=null){
           map.put("grinderId",tGrindingLogEntity.getGrinderId());
            TGrinderEntity tGrinderEntity= tGrinderMessage.selectEntryById(tGrindingLogEntity.getGrinderId());
            if(tGrinderEntity!=null){
                map.put("grinderCode",tGrinderEntity.getCode());
            }
        }
        //上磨床时间
        if(tGrindingLogEntity.getOnGrinderTime()!=null){
           map.put("onGrinderTime",tGrindingLogEntity.getOnGrinderTime());
        }
        //下磨床时间
        if(tGrindingLogEntity.getOffGrinderTime()!=null){
           map.put("offGrinderTime",tGrindingLogEntity.getOffGrinderTime());
        }
        //磨削开始时间
        if(tGrindingLogEntity.getStartGrindingTime()!=null){
           map.put("startGrindingTime",tGrindingLogEntity.getStartGrindingTime());
        }
        //磨削结束时间
        if(tGrindingLogEntity.getEndGrindingTime()!=null){
           map.put("endGrindingTime",tGrindingLogEntity.getEndGrindingTime());
        }
        //磨削去除量
        if(tGrindingLogEntity.getRemovalQuanity()!=null){
           map.put("removalQuanity",tGrindingLogEntity.getRemovalQuanity());
        }
        //砂轮
        if(tGrindingLogEntity.getGrindWheelId()!=null){
           map.put("grindWheelId",tGrindingLogEntity.getGrindWheelId());
           if(tGrindingLogEntity.getGrindWheelId()!=null){
               TGrindWheelBasicEntity tGrindWheelBasicEntity=tGrindWheelBasicService.selectById(tGrindingLogEntity.getGrindWheelId());
               if(tGrindWheelBasicEntity!=null){
                   map.put("grindWheelCode",tGrindWheelBasicEntity.getCode());
                   map.put("grindWheelType",tGrindWheelBasicEntity.getType());
               }

           }
        }
        if(tGrindingLogEntity.getRolledSheet()!=null){
            map.put("rollerSheet",tGrindingLogEntity.getRolledSheet());
        }
        //磨前直径
        if(tGrindingLogEntity.getStartDiameter()!=null){
           map.put("startDiameter",tGrindingLogEntity.getStartDiameter());
        }
        //磨后直径
        if(tGrindingLogEntity.getEndDiameter()!=null){
           map.put("endDiameter",tGrindingLogEntity.getEndDiameter());
        }
        //磨前表面粗糙度
        if(tGrindingLogEntity.getStartRoughness()!=null){
           map.put("startRoughness",tGrindingLogEntity.getStartRoughness());
        }
        //磨后表面粗糙度
        if(tGrindingLogEntity.getEndRoughness()!=null){
           map.put("endRoughness",tGrindingLogEntity.getEndRoughness());
        }
        //轧辊类型
        if(tGrindingLogEntity.getRollerTypeId()!=null){
           map.put("rollerTypeId",tGrindingLogEntity.getRollerTypeId());
        }
        //辊型精度
        if(tGrindingLogEntity.getRollerPrecision()!=null){
           map.put("rollerPrecision",tGrindingLogEntity.getRollerPrecision());
        }
        //磨削程序编码
        if(tGrindingLogEntity.getProgram()!=null){
           map.put("program",tGrindingLogEntity.getProgram());
        }
        //轧制里程
        if(tGrindingLogEntity.getMileage()!=null){
           map.put("mileage",tGrindingLogEntity.getMileage());
        }
        //轧制吨位数
        if(tGrindingLogEntity.getLoad()!=null){
           map.put("load",tGrindingLogEntity.getLoad());
        }
        //磨前直径
        if(tGrindingLogEntity.getStartGrindWheelDiameter()!=null){
           map.put("startGrindWheelDiameter",tGrindingLogEntity.getStartGrindWheelDiameter());
        }
        //磨后直径
        if(tGrindingLogEntity.getEndGrindWheelDiameter()!=null){
           map.put("endGrindWheelDiameter",tGrindingLogEntity.getEndGrindWheelDiameter());
        }
        //
        if(tGrindingLogEntity.getRemark()!=null){
           map.put("remark",tGrindingLogEntity.getRemark());
        }
        //
        if(tGrindingLogEntity.getCreateTime()!=null){
           map.put("createTime",tGrindingLogEntity.getCreateTime());
        }
        //
        if(tGrindingLogEntity.getUpdateTime()!=null){
           map.put("updateTime",tGrindingLogEntity.getUpdateTime());
        }
        //
        if(tGrindingLogEntity.getCreateBy()!=null){
           map.put("createBy",tGrindingLogEntity.getCreateBy());
        }
        //
        if(tGrindingLogEntity.getUpdateBy()!=null){
           map.put("updateBy",tGrindingLogEntity.getUpdateBy());
        }
        return map;
    }





    public Map judgeIsMoveToGrinder(MoveToGrinder moveToGrinder){
        Map map=new HashMap();
//        EntityWrapper<TGrindingLogEntity> tGrindingLogentityWrapper=new EntityWrapper<>();
//        tGrindingLogentityWrapper.eq("roller_id",moveToGrinder.getRollerId()).eq("grinder_id",moveToGrinder.getGrinderId());
//        List<TGrindingLogEntity> list=selectList(tGrindingLogentityWrapper);
//        if(list.size()>0){
//            map.put("msg","该磨床上已经在磨辊");
//            map.put("flag",false);
//            return map;
//        }
        map.put("flag",true);
        return map;

    }
    @Transactional
    @Override
    public void moveToGrinder(MoveToGrinder moveToGrinder) {

        //添加磨床日志
        TGrindingLogEntity tGrindingLogEntity=new TGrindingLogEntity();
        tGrindingLogEntity.setRollerId(moveToGrinder.getRollerId());
        tGrindingLogEntity.setGrinderId(moveToGrinder.getGrinderId());
        tGrindingLogEntity.setRemovalQuanity(moveToGrinder.getRemoveAmount());
        tGrindingLogEntity.setOnGrinderTime(new Date());
        tGrindingLogEntity.setStartDiameter(moveToGrinder.getNowGrindingAmount());
        insert(tGrindingLogEntity);
        //更新轧辊状态
        RollerMoveEntity rollerMoveEntity=new RollerMoveEntity();
        rollerMoveEntity.setRollerId(moveToGrinder.getRollerId());
        rollerMoveEntity.setCode(moveToGrinder.getCode());
        rollerMoveEntity.setWorkingState(moveToGrinder.getWorkingState());
        rollerMoveEntity.setGrinderId(moveToGrinder.getGrinderId());
        if(moveToGrinder.getNowGrindingAmount()!=null){
            rollerMoveEntity.setDiameter(moveToGrinder.getNowGrindingAmount()-moveToGrinder.getRemoveAmount());
        }
        rollerMoveEntity.setSourceRollerBackId(moveToGrinder.getSourceRollerBackId());
        rollerMoveEntity.setSourceRollerRackUorD(moveToGrinder.getSourceRollerRackUorD());
        rollerExtMessage.updateRollerExt(rollerMoveEntity);

        if(moveToGrinder.getProductionIId()!=null){
            TProductionPlanIEntity tProductionPlanIEntity=new TProductionPlanIEntity();
            tProductionPlanIEntity.setId(moveToGrinder.getProductionIId());
            tProductionPlanIEntity.setProductionPlanId(moveToGrinder.getProductionPlanId());
            tProductionPlanIEntity.setSequence(moveToGrinder.getSequence());
            productionPlanIMessage.updateNowProductionPlanIPostion(tProductionPlanIEntity);
            productionPlanIMessage.updateNextProductionPlanIPostion(tProductionPlanIEntity);
        }

    }

    @Override
    public Map selectGrinderState(MoveToGrinder moveToGrinder) {
        synchronized (this) {
            Map map = judgeIsMoveToGrinder(moveToGrinder);
            if (!(Boolean) map.get("flag")) {
                return map;
            }
            //获取当前辊的直径
            Float diameter = rollerExtMessage.selectRollerExtNowDiameter(moveToGrinder.getRollerId());
            //获取磨床的基本磨削值
            Float grindingAmount = tGrinderMessage.selectEntryById(moveToGrinder.getGrinderId()).getGrindingAmount();
            TRollerBasicEntity tRollerBasicEntity = tRollerBasicMessage.selectRollerBasicById(moveToGrinder.getRollerId());

            map.put("flag", true);
            map.put("diameter", diameter);
            map.put("discard_diameter", tRollerBasicEntity.getDiscardDiameter());
            map.put("roughness", tRollerBasicEntity.getRoughness());
            map.put("precision", tRollerBasicEntity.getPrecision());
            map.put("code", tRollerBasicEntity.getCode());
            map.put("removeAmount", grindingAmount);
            return map;
        }
    }


    @Override
    public Map grinderMoveToRoller(GrinderMoveToRoller grinderMoveToRoller) {
        Map map=new HashMap(16);
        RollerMoveEntity rollerMoveEntity=new RollerMoveEntity();
        rollerMoveEntity.setRollerId(grinderMoveToRoller.getRollerId());
        rollerMoveEntity.setTargetRollerBackId(grinderMoveToRoller.getTargetRollerBackId());
        rollerMoveEntity.setTargetRollerRackUorD(grinderMoveToRoller.getTargetRollerRackUorD());
        rollerMoveEntity.setWorkingState(grinderMoveToRoller.getWorkingState());
        rollerMoveEntity.setCode(grinderMoveToRoller.getCode());
        rollerMoveEntity.setGrinderId(grinderMoveToRoller.getGrinderId());
        //判断从磨床是否可以移动轧辊
        map= rollerExtMessage.judgeIsMoveToRollerRacks(rollerMoveEntity);
        if(!(Boolean) map.get("flag")){
            return map;
        }
        //更新磨床到辊架的轧辊状态
        rollerExtMessage.updateGrinderToRollerExt(rollerMoveEntity);
        //添加磨床日志表
        TGrindingLogEntity tGrindingLogEntity=new TGrindingLogEntity();
        tGrindingLogEntity.setOffGrinderTime(grinderMoveToRoller.getOffGrinderTime());
        tGrindingLogEntity.setRollerId(grinderMoveToRoller.getRollerId());
        tGrindingLogEntity.setGrinderId(grinderMoveToRoller.getGrinderId());
        insert(tGrindingLogEntity);
        map.put("flag",true);

        if(grinderMoveToRoller.getProductionIId()!=null){
            TProductionPlanIEntity tProductionPlanIEntity=new TProductionPlanIEntity();
            tProductionPlanIEntity.setId(grinderMoveToRoller.getProductionIId());
            tProductionPlanIEntity.setProductionPlanId(grinderMoveToRoller.getProductionPlanId());
            tProductionPlanIEntity.setSequence(grinderMoveToRoller.getSequence());
            productionPlanIMessage.updateNowProductionPlanIPostion(tProductionPlanIEntity);
            productionPlanIMessage.updateNextProductionPlanIPostion(tProductionPlanIEntity);
        }

        return map;
    }

    @Override
    public Map selectGrinderToRollerDetailReport(Integer rollerId) {
        Map map=new HashMap();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date firstTime=baseMapper.selectFirstGrindTime(rollerId);
        Integer number=baseMapper.selectGrindCount(rollerId);//上磨床次数
        Float allRemoveNumber=baseMapper.selectAllRemoveNumber(rollerId);//磨削总量
        Date endTime=baseMapper.selectEndGrindTime(rollerId);
        if(firstTime!=null){
            map.put("firstTime",simpleDateFormat.format(firstTime));
        }else {
            map.put("firstTime","");
        }
        map.put("endTime",endTime);
        map.put("allRemoveNumber",allRemoveNumber);
        map.put("dis",number*200);
        map.put("time",number);//总磨削时间
        return map;
    }

    @Override
    public List<Map> selectGrinderLogById(Integer id) {
        return baseMapper.selectGrindLogByRollerId(id);
    }

    @Override
    public Map selectGrinderLogDetailById(Integer id) {
        TGrindingLogEntity tGrindingLogEntity=selectById(id);
        if(tGrindingLogEntity!=null){
            Map map=formatEntity(tGrindingLogEntity);
            return map;
        }
        return null;

    }

    @Override
    public List<Map> selectGrinderLogByGrinderId(Integer id) {
        return baseMapper.selectGrindLogByGrinderId(id);
    }

    @Override
    public Map selectGrinderDetailById(Integer id) {

        return null;
    }

}

